<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/admin/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/css/public.css" media="all">
  
    <style>
        body {
            background-color: #ffffff;
        }
        #container {
            width: 100%;
            float: left;
            height: 500px;
            
        } 
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    
    <form id="DataForm" action="" method="POST" class="layui-form layui-form-pane">
        <div class="layui-tab layui-tab-brief" lay-filter="itemTab">

            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">


                    <!-- <div class="layui-form-item">
                        <label class="layui-form-label">用户账号</label>
                        <div class="layui-input-block">
                            <input type="text" name="info[account]" placeholder="请输入用户账号" autocomplete="off"
                                class="layui-input" value="">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">用户密码</label>
                        <div class="layui-input-block">
                            <input type="text" name="info[password]" placeholder="请输入用户密码" autocomplete="off"
                                class="layui-input" value="">
                        </div>
                    </div> -->

                    <div class="layui-form-item">
                        <label class="layui-form-label">团长电话</label>
                        <div class="layui-input-block">
                            <input type="text" name="info[phone]" placeholder="请输入团长电话" autocomplete="off"
                                class="layui-input" value="">
                        </div>
                    </div>

                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">团长昵称</label>
                        <div class="layui-input-block">
                            <input type="text" name="info[nickname]" placeholder="请输入团长昵称" autocomplete="off"
                                class="layui-input" value="">
                        </div>
                    </div>

                    

                    <div class="layui-form-item">
                        <label class="layui-form-label">logo</label>
                        <div class="layui-input-block" >
                            <input type="text" name="info[avatar]" placeholder="请上传logo" autocomplete="off"
                            class="layui-input layui-col-xs6" value="" readonly id="avatar_img">
                     
                            <div class="layuimini-upload-btn">
                                <button type="button" class="layui-btn" id="upload_avatar"><i
                                        class="fa fa-upload"></i>
                                    上传图片</button>
                            </div>
                        </div>
                    </div>
                 

                    <div class="layui-form-item">
                        <label class="layui-form-label">地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="info[address]" placeholder="请输入地址" autocomplete="off"
                            class="layui-input" value="" id="address">
                            <input type="hidden" id="longitude" name="info[longitude]" value="">
                            <input type="hidden" id="latitude" name="info[latitude]" value="">
                            <input type="hidden" id="province" name="province" value="">
                            <input type="hidden" id="city" name="city" value="">
                            <input type="hidden" id="area" name="area" value="">
                            <input type="hidden" name="info[agent_id]" value="{$userInfo['id']}">
                        </div>
                    </div>
                    <div id="container">
            
                    </div>
                
                  
                    <div class="layui-form-item">
                       
                        <div class="layui-input-block" style="margin:auto;text-align: center;">
                            <button id="saveButton" type="submit" class="layui-btn"><i class="fa fa-check"></i>
                                提交</button>
                            <button id="closeButton" type="button" class="layui-btn layui-btn-primary"><i
                                    class="fa fa-close"></i>
                                关闭</button>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </form>
</div>
<script src="/static/admin/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/static/admin/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script type="text/javascript">
    window._AMapSecurityConfig = {
        securityJsCode: "46efe2758da4fc2a94edc2a1407f137d",
    }
  </script>
<script src="https://webapi.amap.com/maps?v=2.0&key=a89f680ef749ca937e04d2ee4fa2643e"></script>

<script>
      layui.use(['layer', 'form', 'laydate', 'element', 'upload','tableSelect','wangEditor'], function () {
        var    tableSelect = layui.tableSelect;
        var $ = layui.$, layer = layui.layer, table = layui.table,  element = layui.element,laydate = layui.laydate, form = layui.form, upload = layui.upload;
        var indexFrame = parent.layer.getFrameIndex(window.name); //获取窗口索引
           //---------------------------地图开始---------------------------
           var map = new AMap.Map('container', {
              viewMode: '2D', // 默认使用 2D 模式，如果希望使用带有俯仰角的 3D 模式，请设置 viewMode: '3D'
              zoom: 11, // 初始化地图层级
              center: [125.317548, 43.904279] ,// 初始化地图中心点
              resizeEnable: true
          });

          var autoOptions = {
              input: "address"
          };
          AMap.plugin(['AMap.PlaceSearch', 'AMap.AutoComplete'], function () {
              var auto = new AMap.AutoComplete(autoOptions);
              var placeSearch = new AMap.PlaceSearch({
                  map: map
              });  //构造地点查询类
              auto.on("select", select);//注册监听，当选中某条记录时会触发
              function select(e) {
                //   placeSearch.setCity(e.poi.adcode);
                  placeSearch.search(e.poi.name);  //关键字查询查询
              }
          })

          var clickHandler = function (e) {
              addMarker(e.lnglat.getLng(), e.lnglat.getLat())
              $.ajax({
                  url: "{:url('gaode_address')}",
                  type: 'post',
                  data: {
                      'longitude': e.lnglat.getLng(),
                      'latitude': e.lnglat.getLat(),
                  },
                  success: function (data) {
                  
                     $("#address").val(data.data)
                     $("#longitude").val(e.lnglat.getLng())
                     $("#latitude").val(e.lnglat.getLat())
                  }
              })
          };
          //绑定事件
          map.on('click', clickHandler);
          //添加点标记
          var marker = '';
          function addMarker(lon, lat) {
              if (marker) {
                  //删除点标记
                  marker.setMap(null);
              }
              marker = new AMap.Marker({
                  icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
                  position: [lon, lat],
                  offset: new AMap.Pixel(-13, -30)
              });
              marker.setMap(map);

          }

        //---------------------------地图结束---------------------------
        $("#saveButton").on('click', function () {
            var loading = layer.open({ type: 3 });
            var data = $("#DataForm").serialize()
          
            $.ajax({
                url: "{:url('add')}",
                type: 'post',
                data:data,
                success: function (data) {
                    console.log(data);
                    layer.close(loading);
                    if (data.code == 0) {
                        parent.tableReload();
                        parent.layer.close(indexFrame);
                        parent.layer.msg(data.msg);
                    } else {
                        layer.msg(data.msg);
                    }
                }
            })
            return false;
        })
        upload.render({
                elem: '#upload_image', url: "{:url('common/images')}", done: function (res) {
                        $("#avatar_img").val(res.data.name);
                        layer.msg(res.msg);
                }
        });
        $("#closeButton").on('click', function () {
            parent.layer.close(indexFrame);
        })
        
        form.on('select(city)', function (data) {
                var address_id = parseInt($("#" + data.elem.id).attr('data-id'));
                var num = parseInt($("#" + data.elem.id).attr('data-num'));
                var level = 3; //联动级别
                var next = num + 1;
                for (var i = next; i < level; i++) $("option[name='select_" + i + "']").remove();
                if ($('#select_' + num + '_' + address_id).val() != "") {
                    $.post("{:url('common/get_city')}", { id: data.value }, function (data) {
                        for (var i = 0; i < data.length; i++) {
                            var option_str = '<option name="select_' + next + '" value="' + data[i].id + '">' + data[i].Name + '</option>';
                            $('#select_' + next + '_' + address_id).append(option_str);
                        }
                        form.render();
                    });
                }
                form.render();
            });
           
            upload.render({
                elem: '#upload_avatar', url: "{:url('common/images')}", done: function (res) {
                        $("#avatar_img").val(res.data.name);
                        layer.msg(res.msg);
                }
            });
            });
</script>
</body>
</html>